package tagcloud.gui;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import java.awt.Insets;
import java.awt.Dimension;
import java.util.ArrayList;

import tagcloud.core.*;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class WordSelectionPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel descriptionLabel = null;
	private JScrollPane scrollPane = null;
	private JPanel gridPanel = null;
	private JLabel wordLabel = null;
	private JLabel countLabel = null;
	private JLabel invisLabel = null;
	private JPanel borderPanel = null;
	private JPanel bottomPanel = null;
	private JButton backButton = null;
	private JButton generateButton = null;
	
	private ActionListener parentActionListener;
	
	private ArrayList<WordElementPane> wordElements = new ArrayList<WordElementPane>();
	private int wordElementY = 0;
	
	/**
	 * This is the default constructor
	 */
	public WordSelectionPanel(ActionListener parentActionListener) {
		super();
		this.parentActionListener = parentActionListener;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
		gridBagConstraints13.gridx = 0;
		gridBagConstraints13.gridwidth = 3;
		gridBagConstraints13.weightx = 1.0D;
		gridBagConstraints13.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints13.insets = new Insets(4, 0, 0, 0);
		gridBagConstraints13.gridy = 3;
		GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
		gridBagConstraints12.gridx = 0;
		gridBagConstraints12.gridy = 1;
		invisLabel = new JLabel();
		invisLabel.setPreferredSize(new Dimension(25, 21));
		invisLabel.setIcon(new ImageIcon(getClass().getResource("/images/gap.png")));
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.gridx = 2;
		gridBagConstraints11.weightx = 1.0D;
		gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints11.gridy = 1;
		countLabel = new JLabel();
		countLabel.setText("Count");
		GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
		gridBagConstraints10.gridx = 1;
		gridBagConstraints10.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints10.weightx = 1.0D;
		gridBagConstraints10.gridy = 1;
		wordLabel = new JLabel();
		wordLabel.setText("Word");
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.fill = GridBagConstraints.BOTH;
		gridBagConstraints1.gridy = 2;
		gridBagConstraints1.weightx = 1.0;
		gridBagConstraints1.weighty = 1.0;
		gridBagConstraints1.gridwidth = 4;
		gridBagConstraints1.gridx = 0;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0D;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.gridy = 0;
		descriptionLabel = new JLabel();
		descriptionLabel.setText("Select the words below that you wish to be part of the Tag Cloud:");
		this.setSize(640, 480);
		this.setLayout(new GridBagLayout());
		this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		this.setName("WordSelection Panel");
		this.add(descriptionLabel, gridBagConstraints);
		this.add(getScrollPane(), gridBagConstraints1);
		this.add(wordLabel, gridBagConstraints10);
		this.add(countLabel, gridBagConstraints11);
		this.add(invisLabel, gridBagConstraints12);
		this.add(getBottomPanel(), gridBagConstraints13);
		
		for(int i = 50; i > 0; --i) {
			Word temp = new Word("Test" + (51 - i));
			temp.setNumOccurences(i);
			addWordElement(temp, (i > 40));
		}
	}

	/**
	 * This method initializes scrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			scrollPane.setViewportView(getBorderPanel());
		}
		return scrollPane;
	}

	/**
	 * This method initializes gridPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getGridPanel() {
		if (gridPanel == null) {
			gridPanel = new JPanel();
			gridPanel.setLayout(new GridBagLayout());
		}
		return gridPanel;
	}
	
	public void addWordElements(Word[] words, int selectTopCount) {
		for(int i = 0; i < words.length; ++i) {
			addWordElement(words[i], (i < selectTopCount));
		}
	}
	
	public void addWordElement(Word word, boolean selected) {
		GridBagConstraints tempConstraints = new GridBagConstraints();
		tempConstraints.gridx = 0;
		tempConstraints.fill = GridBagConstraints.HORIZONTAL;
		tempConstraints.weightx = 1.0D;
		tempConstraints.anchor = GridBagConstraints.CENTER;
		tempConstraints.gridy = wordElementY++;
		
		WordElementPane tempPane = new WordElementPane(word, selected);
		wordElements.add(tempPane);
		
		gridPanel.add(tempPane, tempConstraints);
	}
	
	public void clearWordElements() {
		gridPanel.removeAll();
		wordElements.clear();
		wordElementY = 0;
	}

	/**
	 * This method initializes borderPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getBorderPanel() {
		if (borderPanel == null) {
			borderPanel = new JPanel();
			borderPanel.setLayout(new BorderLayout());
			borderPanel.add(getGridPanel(), BorderLayout.NORTH);
		}
		return borderPanel;
	}

	/**
	 * This method initializes bottomPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getBottomPanel() {
		if (bottomPanel == null) {
			bottomPanel = new JPanel();
			bottomPanel.setLayout(new BorderLayout());
			bottomPanel.add(getBackButton(), BorderLayout.WEST);
			bottomPanel.add(getGenerateButton(), BorderLayout.EAST);
		}
		return bottomPanel;
	}

	/**
	 * This method initializes backButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	protected JButton getBackButton() {
		if (backButton == null) {
			backButton = new JButton();
			backButton.setText("<< Back");
			backButton.addActionListener(parentActionListener);
		}
		return backButton;
	}

	/**
	 * This method initializes generateButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	protected JButton getGenerateButton() {
		if (generateButton == null) {
			generateButton = new JButton();
			generateButton.setText("Generate");
			generateButton.addActionListener(parentActionListener);
		}
		return generateButton;
	}

}
